package com.alex.statistics.controller;

import com.alex.statistics.method.descriptive.CentralTendencyAnalyzer;
import com.alex.statistics.method.descriptive.DispersionAnalyzer;
import com.alex.statistics.method.descriptive.DistributionAnalyzer;
import com.alex.statistics.pojo.request.descriptive.DistributionAnalysisRequest;
import com.alex.statistics.pojo.result.descriptive.CentralTendencyResult;
import com.alex.statistics.pojo.result.descriptive.DispersionResult;
import com.alex.statistics.pojo.Result;
import com.alex.statistics.pojo.result.descriptive.DistributionAnalysisResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "描述性分析")
@RequestMapping("/descriptive")  // 确保类或方法有路径声明
@RestController
public class DescriptiveController {
    @Autowired
    CentralTendencyAnalyzer centralTendencyAnalyzer;
    @Autowired
    DispersionAnalyzer dispersionAnalyzer;
    @Autowired
    DistributionAnalyzer distributionAnalyzer;

    @Operation(summary = "集中趋势分析")  // Swagger 方法描述
    @PostMapping("/centralTendency")  // 必须有路径
    public Result<CentralTendencyResult> centralTendency(@RequestBody @Schema(example = "[150.0, 120.0, 80.0, 70.0, 60.0, 50.0, 40.0, 30.0, 20.0, 10.0, 145.0, 12.0, 54.0, 56.0, 44.0, 40.0, 45.0, 100.0, 15.0, 20.0]") List<Double> data) {
        return Result.ok(centralTendencyAnalyzer.analyze(data));
    }

    @Operation(summary = "离散程度分析")  // Swagger 方法描述
    @PostMapping("/dispersion")  // 必须有路径
    public Result<DispersionResult> dispersion(@RequestBody @Schema(example = "[150.0, 120.0, 80.0, 70.0, 60.0, 50.0, 40.0, 30.0, 20.0, 10.0, 145.0, 12.0, 54.0, 56.0, 44.0, 40.0, 45.0, 100.0, 15.0, 20.0]") List<Double> data) {
        return Result.ok(dispersionAnalyzer.analyze(data));
    }

    @Operation(summary = "分布形态分析")
    @PostMapping("/distribution")
    public Result<DistributionAnalysisResult> fullAnalysis(
            @RequestBody DistributionAnalysisRequest request) {
        double[] data = request.getData().stream().mapToDouble(Double::doubleValue).toArray();
        double[] bins = request.getBins().stream().mapToDouble(Double::doubleValue).toArray();

        DistributionAnalysisResult response = new DistributionAnalysisResult();
        response.setFrequencyDistribution(distributionAnalyzer.frequencyDistribution(data, bins));
        response.setSkewness(distributionAnalyzer.skewness(data));
        response.setKurtosis(distributionAnalyzer.kurtosis(data));

        return Result.ok(response);
    }
}
